package net.bmwgroup.easyrule.demo.listener;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.RuleListener;

public class BusinessRuleListener implements RuleListener {
    private Long startTime;

    @Override
    public boolean beforeEvaluate(Rule rule, Facts facts) {
        System.err.println("Rule: " + rule.getName() + " beforeEvaluate start ...");
        startTime = System.currentTimeMillis();
        return RuleListener.super.beforeEvaluate(rule, facts);
    }

    @Override
    public void afterEvaluate(Rule rule, Facts facts, boolean evaluationResult) {
        System.err.println("Rule: " + rule.getName() + " afterEvaluate start ... Elapsed Time: " + (System.currentTimeMillis() - startTime) + " milliseconds");
        RuleListener.super.afterEvaluate(rule, facts, evaluationResult);
    }

    @Override
    public void onEvaluationError(Rule rule, Facts facts, Exception exception) {
        RuleListener.super.onEvaluationError(rule, facts, exception);
    }

    @Override
    public void beforeExecute(Rule rule, Facts facts) {
        System.err.println("Rule: " + rule.getName() + " beforeExecute start ... ");
        //startTime = System.currentTimeMillis();
        RuleListener.super.beforeExecute(rule, facts);
    }

    @Override
    public void onSuccess(Rule rule, Facts facts) {
        System.err.println("Rule: " + rule.getName() + " onSuccess start ... Elapsed Time: " + (System.currentTimeMillis() - startTime) + " milliseconds");
        RuleListener.super.onSuccess(rule, facts);
    }

    @Override
    public void onFailure(Rule rule, Facts facts, Exception exception) {
        RuleListener.super.onFailure(rule, facts, exception);
    }
}
